<template lang="pug">
  .page
    .AccountsReceivableManage
      .search-form.mb-10.pl-10.pr-10
        Form.ivu-row(ref="formData" :model="formData" :rules="rules" :label-width="90")
          FormItem.form-item.ivu-col.pr-20.mb-15(v-for="(item, index) of formItem" :label="item.label" :key="index" :class="item.style" :prop="item.prop")
            template(v-if="item.type === 'input'")
              Input(v-model="formData[item.model]" :placeholder="item.placeholder")
            template(v-if="item.type === 'button'")
              Button(type="info" @click="searchFn") 查询

      Table.hasTitle-table.beautiful-table(:columns="thead" :data="tableData" :stripe="true" ref="demoTable")
        .clearfix.pr-24.pl-12.overflow-hidden(slot="header" v-if="hasImport")
          .pull-right.table-title-btns
            Button.mr-10(v-if="hasImport" type="info" @click="handleShowBatchImportModal") 导入
        template(slot-scope="{ row, index }" slot="action")
          Button.mr-10(v-if="hasViewDetail" type="text" size="small" @click="goToDetails(row.id)") 明细
          Button(v-if="hasSubmit" type="text" size="small" @click="handleSubmit(row.id)" :disabled="row.status === 2") 提交财务
        .clearfix.pl-20.pr-20(slot="footer")
          Page(
            :total="tableComone.totalRows"
            @on-change="onChangePage"
            @on-page-size-change="onpageSizeChange"
            show-sizer
            show-total
            placement="top"
            prev-text="上一页"
            next-text="下一页"
            :page-size-opts="[10, 20, 30, 50, 100]"
          )

    //- 批量导入弹窗
    BatchImportModal(ref="BatchImportModal" @query="searchFn")
</template>

<script>
import { EnumApi } from '@/api/Enum.api.js'
import { FinanceBasedataApi } from '@/api/FinanceBasedata.api.js'
// import { BalanceApi } from '@/api/Balance.api.js'
import { ServersThead } from './tableHead.js'
import _cloneDeep from 'clone-deep'
import { EmployeesApi } from '@/api/Employees.api.js'
import { Common } from '@/utils/common.js'
import BatchImportModal from './modal/batchImportModal.vue'

export default {
  name: '',

  components: {
    BatchImportModal
  },

  data () {
    return {
      companyId: '', // 公司ID
      companyName: '', // 公司名称
      employeeId: '', // 员工ID
      thead: _cloneDeep(ServersThead),
      tableData: [],
      formData: {
        orderNo: ''
      },
      formItem: [
        { type: 'input', label: '流水号：', placeholder: '请输入流水号', prop: 'orderNo', model: 'orderNo', options: 'customer', style: 'ivu-col-span-md-24 ivu-col-span-lg-12 ivu-col-span-xl-8 ivu-col-span-xxl-6' },
        { type: 'button', style: 'ivu-col-span-md-24 ivu-col-span-lg-12 ivu-col-span-xl-8 ivu-col-span-xxl-6' }
      ],
      rules: {},
      selectedEmployeeId: [], // 勾选的id集合,array格式
      selectedEmployeeArr: [], // 勾选的id, name数组集合
      tableComone: {
        pageIndex: 1, // 页码
        totalRows: 0, // 总条数
        pageSize: 10, // 当前页面展示条数
        inServiceCount: 0, // 在职
        outServiceCount: 0 // 离职
      },
      downLoadRul: '',
      arrListDown: {
        customer: []
      },
      datePickerDefault: new Date(), // 当前时间
      importExcel: {
        uploadLoading: false,
        progressPercent: 0,
        showProgress: false,
        showRemoveFile: false,
        file: null
      }
    }
  },
  computed: {
    actionArr () {
      let arr = this.$store.state.user.actionAccess.map(item => {
        console.log('item: ', item)
        return item.code
      })
      return arr
    },
    // 导入
    hasImport () {
      return this.actionArr.indexOf('ImportFinance') > -1
    },
    // 明细
    hasViewDetail () {
      return this.actionArr.indexOf('ViewFinanceDetail') > -1
    },
    // 提交财务
    hasSubmit () {
      return this.actionArr.indexOf('SubmitFinance') > -1
    }
  },

  watch: {
  },

  mounted () {
    this.query()
  },

  methods: {
    /**
     * 分页页码变动
     * @param {Number} val pageIndex
     */
    onChangePage (val) {
      this.tableComone.pageIndex = val
      this.query()
    },
    /**
     * 分页页码变动
     * @param {Number} val pageSize
     */
    onpageSizeChange (val) {
      this.tableComone.pageIndex = 1
      this.tableComone.pageSize = val
      this.query()
    },

    // 搜索
    searchFn () {
      this.tableComone.pageIndex = 1
      this.query()
    },

    /**
     * 分页查询接口
     * @param {Object} obj {pageIndex, pageSize}
     */
    async query () {
      var params = {
        OrderNo: this.formData.orderNo,
        CustomerId: this.$route.query.customerId,
        PageIndex: this.tableComone.pageIndex,
        PageSize: this.tableComone.pageSize
      }
      let res = await FinanceBasedataApi.getList(params)
      console.log('客户差额详情res: ', res)
      if (res.status === 200 && res.data.success) {
        this.tableData = res.data.data
        this.tableComone.totalRows = res.data.totalRows
      } else {
        this.tableData = []
      }
      this.loading = false
    },

    // 表格选择操作
    handleOnSelectChange (value) {
      this.selectedEmployeeArr = value.map((item) => {
        return { id: item.id, name: item.name }
      })
      this.selectedEmployeeId = value.map((item) => {
        return item.id
      })
      console.log('列表勾选数组1', this.selectedEmployeeArr)
      console.log('列表勾选数组2', this.selectedEmployeeId)
    },

    // 批量导入-显示弹窗
    handleShowBatchImportModal () {
      this.$refs.BatchImportModal.file = null
      this.$refs.BatchImportModal.errorData.isError = false
      this.$refs.BatchImportModal.isShowBatchImportModal = true
      this.$refs.BatchImportModal.customerId = this.$route.query.customerId
    },

    // 提交财务
    async handleSubmit (id) {
      console.log('id: ', id)
      let res = await FinanceBasedataApi.submit({ id: id })
      console.log('提交财务res: ', res)
      if (res.status === 200 && res.data.success) {
        this.$Message.success(res.data.message)
        this.query()
      } else {
        this.$Message.error({
          content: res.data.message,
          duration: 10,
          closable: true
        })
      }
    },

    // 去往详情
    goToDetails (id) {
      this.$router.push({
        path: '/unionPayServersDetails',
        name: 'unionPayServersDetails',
        query: {
          id: id
        }
      })
    }
  } // methods end
}
</script>

<style lang="less" scoped>

</style>
